<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>events.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_events.js.html">events.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Event Manager.
<a name="line17"></a> *
<a name="line18"></a> * Provides an abstracted interface to the browsers&#39; event
<a name="line19"></a> * systems. This uses an indirect lookup of listener functions to avoid circular
<a name="line20"></a> * references between DOM (in IE) or XPCOM (in Mozilla) objects which leak
<a name="line21"></a> * memory. This makes it easier to write OO Javascript/DOM code.
<a name="line22"></a> *
<a name="line23"></a> * It simulates capture &amp; bubble in Internet Explorer.
<a name="line24"></a> *
<a name="line25"></a> * The listeners will also automagically have their event objects patched, so
<a name="line26"></a> * your handlers don&#39;t need to worry about the browser.
<a name="line27"></a> *
<a name="line28"></a> * Example usage:
<a name="line29"></a> * &lt;pre&gt;
<a name="line30"></a> * goog.events.listen(myNode, &#39;click&#39;, function(e) { alert(&#39;woo&#39;) });
<a name="line31"></a> * goog.events.listen(myNode, &#39;mouseover&#39;, mouseHandler, true);
<a name="line32"></a> * goog.events.unlisten(myNode, &#39;mouseover&#39;, mouseHandler, true);
<a name="line33"></a> * goog.events.removeAll(myNode);
<a name="line34"></a> * goog.events.removeAll();
<a name="line35"></a> * &lt;/pre&gt;
<a name="line36"></a> *
<a name="line37"></a> *                                            in IE and event object patching]
<a name="line38"></a> *
<a name="line39"></a> * @supported IE6+, FF1.5+, WebKit, Opera.
<a name="line40"></a> * @see ../demos/events.html
<a name="line41"></a> * @see ../demos/event-propagation.html
<a name="line42"></a> * @see ../demos/stopevent.html
<a name="line43"></a> */
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>// This uses 3 lookup tables/trees.
<a name="line47"></a>// listenerTree_ is a tree of type -&gt; capture -&gt; src uid -&gt; [Listener]
<a name="line48"></a>// listeners_ is a map of key -&gt; [Listener]
<a name="line49"></a>//
<a name="line50"></a>// The key is a field of the Listener. The Listener class also has the type,
<a name="line51"></a>// capture and the src so one can always trace back in the tree
<a name="line52"></a>//
<a name="line53"></a>// sources_: src uid -&gt; [Listener]
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>goog.provide(&#39;goog.events&#39;);
<a name="line57"></a>goog.provide(&#39;goog.events.Key&#39;);
<a name="line58"></a>
<a name="line59"></a>goog.require(&#39;goog.array&#39;);
<a name="line60"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line61"></a>goog.require(&#39;goog.debug.errorHandlerWeakDep&#39;);
<a name="line62"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line63"></a>goog.require(&#39;goog.events.BrowserFeature&#39;);
<a name="line64"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line65"></a>goog.require(&#39;goog.events.EventWrapper&#39;);
<a name="line66"></a>goog.require(&#39;goog.events.Listenable&#39;);
<a name="line67"></a>goog.require(&#39;goog.events.Listener&#39;);
<a name="line68"></a>goog.require(&#39;goog.object&#39;);
<a name="line69"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * @typedef {number|goog.events.ListenableKey}
<a name="line74"></a> */
<a name="line75"></a>goog.events.Key;
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * @typedef {EventTarget|goog.events.Listenable|goog.events.EventTarget}
<a name="line80"></a> */
<a name="line81"></a>goog.events.ListenableType;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Container for storing event listeners and their proxies
<a name="line86"></a> * @private
<a name="line87"></a> * @type {Object.&lt;goog.events.ListenableKey&gt;}
<a name="line88"></a> */
<a name="line89"></a>goog.events.listeners_ = {};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * The root of the listener tree
<a name="line94"></a> * @private
<a name="line95"></a> * @type {Object}
<a name="line96"></a> */
<a name="line97"></a>goog.events.listenerTree_ = {};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Lookup for mapping source UIDs to listeners.
<a name="line102"></a> * @private
<a name="line103"></a> * @type {Object}
<a name="line104"></a> */
<a name="line105"></a>goog.events.sources_ = {};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * String used to prepend to IE event types.  Not a constant so that it is not
<a name="line110"></a> * inlined.
<a name="line111"></a> * @type {string}
<a name="line112"></a> * @private
<a name="line113"></a> */
<a name="line114"></a>goog.events.onString_ = &#39;on&#39;;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Map of computed on strings for IE event types. Caching this removes an extra
<a name="line119"></a> * object allocation in goog.events.listen which improves IE6 performance.
<a name="line120"></a> * @type {Object}
<a name="line121"></a> * @private
<a name="line122"></a> */
<a name="line123"></a>goog.events.onStringMap_ = {};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Separator used to split up the various parts of an event key, to help avoid
<a name="line128"></a> * the possibilities of collisions.
<a name="line129"></a> * @type {string}
<a name="line130"></a> * @private
<a name="line131"></a> */
<a name="line132"></a>goog.events.keySeparator_ = &#39;_&#39;;
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Adds an event listener for a specific event on a DOM Node or an
<a name="line137"></a> * object that has implemented {@link goog.events.EventTarget}. A
<a name="line138"></a> * listener can only be added once to an object and if it is added
<a name="line139"></a> * again the key for the listener is returned. Note that if the
<a name="line140"></a> * existing listener is a one-off listener (registered via
<a name="line141"></a> * listenOnce), it will no longer be a one-off listener after a call
<a name="line142"></a> * to listen().
<a name="line143"></a> *
<a name="line144"></a> * @param {goog.events.ListenableType} src The node to listen to
<a name="line145"></a> *     events on.
<a name="line146"></a> * @param {string|Array.&lt;string&gt;} type Event type or array of event types.
<a name="line147"></a> * @param {Function|Object} listener Callback method, or an object with a
<a name="line148"></a> *     handleEvent function.
<a name="line149"></a> * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
<a name="line150"></a> *     false).
<a name="line151"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line152"></a> * @return {goog.events.Key} Unique key for the listener.
<a name="line153"></a> */
<a name="line154"></a>goog.events.listen = function(src, type, listener, opt_capt, opt_handler) {
<a name="line155"></a>  if (goog.isArray(type)) {
<a name="line156"></a>    for (var i = 0; i &lt; type.length; i++) {
<a name="line157"></a>      goog.events.listen(src, type[i], listener, opt_capt, opt_handler);
<a name="line158"></a>    }
<a name="line159"></a>    return null;
<a name="line160"></a>  }
<a name="line161"></a>
<a name="line162"></a>  var listenableKey;
<a name="line163"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line164"></a>      goog.events.Listenable.isImplementedBy(src)) {
<a name="line165"></a>    listenableKey = src.listen(
<a name="line166"></a>        /** @type {string} */ (type),
<a name="line167"></a>        goog.events.wrapListener_(listener), opt_capt, opt_handler);
<a name="line168"></a>  } else {
<a name="line169"></a>    listenableKey = goog.events.listen_(
<a name="line170"></a>        /** @type {EventTarget|goog.events.EventTarget} */ (src),
<a name="line171"></a>        type, listener, /* callOnce */ false, opt_capt, opt_handler);
<a name="line172"></a>  }
<a name="line173"></a>
<a name="line174"></a>  var key = listenableKey.key;
<a name="line175"></a>  goog.events.listeners_[key] = listenableKey;
<a name="line176"></a>  return key;
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>/**
<a name="line181"></a> * Adds an event listener for a specific event on a DOM Node or an object that
<a name="line182"></a> * has implemented {@link goog.events.EventTarget}. A listener can only be
<a name="line183"></a> * added once to an object and if it is added again the key for the listener
<a name="line184"></a> * is returned.
<a name="line185"></a> *
<a name="line186"></a> * Note that a one-off listener will not change an existing listener,
<a name="line187"></a> * if any. On the other hand a normal listener will change existing
<a name="line188"></a> * one-off listener to become a normal listener.
<a name="line189"></a> *
<a name="line190"></a> * @param {EventTarget|goog.events.EventTarget} src The node to listen to
<a name="line191"></a> *     events on.
<a name="line192"></a> * @param {?string} type Event type or array of event types.
<a name="line193"></a> * @param {Function|Object} listener Callback method, or an object with a
<a name="line194"></a> *     handleEvent function.
<a name="line195"></a> * @param {boolean} callOnce Whether the listener is a one-off
<a name="line196"></a> *     listener or otherwise.
<a name="line197"></a> * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
<a name="line198"></a> *     false).
<a name="line199"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line200"></a> * @return {goog.events.ListenableKey} Unique key for the listener.
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>goog.events.listen_ = function(
<a name="line204"></a>    src, type, listener, callOnce, opt_capt, opt_handler) {
<a name="line205"></a>  if (!type) {
<a name="line206"></a>    throw Error(&#39;Invalid event type&#39;);
<a name="line207"></a>  }
<a name="line208"></a>
<a name="line209"></a>  var capture = !!opt_capt;
<a name="line210"></a>  var map = goog.events.listenerTree_;
<a name="line211"></a>
<a name="line212"></a>  if (!(type in map)) {
<a name="line213"></a>    map[type] = {count_: 0, remaining_: 0};
<a name="line214"></a>  }
<a name="line215"></a>  map = map[type];
<a name="line216"></a>
<a name="line217"></a>  if (!(capture in map)) {
<a name="line218"></a>    map[capture] = {count_: 0, remaining_: 0};
<a name="line219"></a>    map.count_++;
<a name="line220"></a>  }
<a name="line221"></a>  map = map[capture];
<a name="line222"></a>
<a name="line223"></a>  var srcUid = goog.getUid(src);
<a name="line224"></a>  var listenerArray, listenerObj;
<a name="line225"></a>
<a name="line226"></a>  // The remaining_ property is used to be able to short circuit the iteration
<a name="line227"></a>  // of the event listeners.
<a name="line228"></a>  //
<a name="line229"></a>  // Increment the remaining event listeners to call even if this event might
<a name="line230"></a>  // already have been fired. At this point we do not know if the event has
<a name="line231"></a>  // been fired and it is too expensive to find out. By incrementing it we are
<a name="line232"></a>  // guaranteed that we will not skip any event listeners.
<a name="line233"></a>  map.remaining_++;
<a name="line234"></a>
<a name="line235"></a>  // Do not use srcUid in map here since that will cast the number to a
<a name="line236"></a>  // string which will allocate one string object.
<a name="line237"></a>  if (!map[srcUid]) {
<a name="line238"></a>    listenerArray = map[srcUid] = [];
<a name="line239"></a>    map.count_++;
<a name="line240"></a>  } else {
<a name="line241"></a>    listenerArray = map[srcUid];
<a name="line242"></a>    // Ensure that the listeners do not already contain the current listener
<a name="line243"></a>    for (var i = 0; i &lt; listenerArray.length; i++) {
<a name="line244"></a>      listenerObj = listenerArray[i];
<a name="line245"></a>      if (listenerObj.listener == listener &amp;&amp;
<a name="line246"></a>          listenerObj.handler == opt_handler) {
<a name="line247"></a>
<a name="line248"></a>        // If this listener has been removed we should not return its key. It
<a name="line249"></a>        // is OK that we create new listenerObj below since the removed one
<a name="line250"></a>        // will be cleaned up later.
<a name="line251"></a>        if (listenerObj.removed) {
<a name="line252"></a>          break;
<a name="line253"></a>        }
<a name="line254"></a>
<a name="line255"></a>        if (!callOnce) {
<a name="line256"></a>          // Ensure that, if there is an existing callOnce listener, it is no
<a name="line257"></a>          // longer a callOnce listener.
<a name="line258"></a>          listenerArray[i].callOnce = false;
<a name="line259"></a>        }
<a name="line260"></a>
<a name="line261"></a>        // We already have this listener. Return its key.
<a name="line262"></a>        return listenerArray[i];
<a name="line263"></a>      }
<a name="line264"></a>    }
<a name="line265"></a>  }
<a name="line266"></a>
<a name="line267"></a>  var proxy = goog.events.getProxy();
<a name="line268"></a>  listenerObj = new goog.events.Listener();
<a name="line269"></a>  listenerObj.init(listener, proxy, src, type, capture, opt_handler);
<a name="line270"></a>  listenerObj.callOnce = callOnce;
<a name="line271"></a>
<a name="line272"></a>  proxy.src = src;
<a name="line273"></a>  proxy.listener = listenerObj;
<a name="line274"></a>
<a name="line275"></a>  listenerArray.push(listenerObj);
<a name="line276"></a>
<a name="line277"></a>  if (!goog.events.sources_[srcUid]) {
<a name="line278"></a>    goog.events.sources_[srcUid] = [];
<a name="line279"></a>  }
<a name="line280"></a>  goog.events.sources_[srcUid].push(listenerObj);
<a name="line281"></a>
<a name="line282"></a>  // Attach the proxy through the browser&#39;s API
<a name="line283"></a>  if (src.addEventListener) {
<a name="line284"></a>    if (src == goog.global || !src.customEvent_) {
<a name="line285"></a>      src.addEventListener(type, proxy, capture);
<a name="line286"></a>    }
<a name="line287"></a>  } else {
<a name="line288"></a>    // The else above used to be else if (src.attachEvent) and then there was
<a name="line289"></a>    // another else statement that threw an exception warning the developer
<a name="line290"></a>    // they made a mistake. This resulted in an extra object allocation in IE6
<a name="line291"></a>    // due to a wrapper object that had to be implemented around the element
<a name="line292"></a>    // and so was removed.
<a name="line293"></a>    src.attachEvent(goog.events.getOnString_(type), proxy);
<a name="line294"></a>  }
<a name="line295"></a>
<a name="line296"></a>  return listenerObj;
<a name="line297"></a>};
<a name="line298"></a>
<a name="line299"></a>
<a name="line300"></a>/**
<a name="line301"></a> * Helper function for returning a proxy function.
<a name="line302"></a> * @return {Function} A new or reused function object.
<a name="line303"></a> */
<a name="line304"></a>goog.events.getProxy = function() {
<a name="line305"></a>  var proxyCallbackFunction = goog.events.handleBrowserEvent_;
<a name="line306"></a>  // Use a local var f to prevent one allocation.
<a name="line307"></a>  var f = goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT ?
<a name="line308"></a>      function(eventObject) {
<a name="line309"></a>        return proxyCallbackFunction.call(f.src, f.listener, eventObject);
<a name="line310"></a>      } :
<a name="line311"></a>      function(eventObject) {
<a name="line312"></a>        var v = proxyCallbackFunction.call(f.src, f.listener, eventObject);
<a name="line313"></a>        // NOTE(user): In IE, we hack in a capture phase. However, if
<a name="line314"></a>        // there is inline event handler which tries to prevent default (for
<a name="line315"></a>        // example &lt;a href=&quot;...&quot; onclick=&quot;return false&quot;&gt;...&lt;/a&gt;) in a
<a name="line316"></a>        // descendant element, the prevent default will be overridden
<a name="line317"></a>        // by this listener if this listener were to return true. Hence, we
<a name="line318"></a>        // return undefined.
<a name="line319"></a>        if (!v) return v;
<a name="line320"></a>      };
<a name="line321"></a>  return f;
<a name="line322"></a>};
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/**
<a name="line326"></a> * Adds an event listener for a specific event on a DomNode or an object that
<a name="line327"></a> * has implemented {@link goog.events.EventTarget}. After the event has fired
<a name="line328"></a> * the event listener is removed from the target.
<a name="line329"></a> *
<a name="line330"></a> * If an existing listener already exists, listenOnce will do
<a name="line331"></a> * nothing. In particular, if the listener was previously registered
<a name="line332"></a> * via listen(), listenOnce() will not turn the listener into a
<a name="line333"></a> * one-off listener. Similarly, if there is already an existing
<a name="line334"></a> * one-off listener, listenOnce does not modify the listeners (it is
<a name="line335"></a> * still a once listener).
<a name="line336"></a> *
<a name="line337"></a> * @param {goog.events.ListenableType} src The node to listen to
<a name="line338"></a> *     events on.
<a name="line339"></a> * @param {string|Array.&lt;string&gt;} type Event type or array of event types.
<a name="line340"></a> * @param {Function|Object} listener Callback method.
<a name="line341"></a> * @param {boolean=} opt_capt Fire in capture phase?.
<a name="line342"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line343"></a> * @return {goog.events.Key} Unique key for the listener.
<a name="line344"></a> */
<a name="line345"></a>goog.events.listenOnce = function(src, type, listener, opt_capt, opt_handler) {
<a name="line346"></a>  if (goog.isArray(type)) {
<a name="line347"></a>    for (var i = 0; i &lt; type.length; i++) {
<a name="line348"></a>      goog.events.listenOnce(src, type[i], listener, opt_capt, opt_handler);
<a name="line349"></a>    }
<a name="line350"></a>    return null;
<a name="line351"></a>  }
<a name="line352"></a>
<a name="line353"></a>  var listenableKey;
<a name="line354"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line355"></a>      goog.events.Listenable.isImplementedBy(src)) {
<a name="line356"></a>    listenableKey = src.listenOnce(
<a name="line357"></a>        /** @type {string} */ (type),
<a name="line358"></a>        goog.events.wrapListener_(listener), opt_capt, opt_handler);
<a name="line359"></a>  } else {
<a name="line360"></a>    listenableKey = goog.events.listen_(
<a name="line361"></a>        /** @type {EventTarget|goog.events.EventTarget} */ (src),
<a name="line362"></a>        type, listener, /* callOnce */ true, opt_capt, opt_handler);
<a name="line363"></a>  }
<a name="line364"></a>
<a name="line365"></a>  var key = listenableKey.key;
<a name="line366"></a>  goog.events.listeners_[key] = listenableKey;
<a name="line367"></a>  return key;
<a name="line368"></a>};
<a name="line369"></a>
<a name="line370"></a>
<a name="line371"></a>/**
<a name="line372"></a> * Adds an event listener with a specific event wrapper on a DOM Node or an
<a name="line373"></a> * object that has implemented {@link goog.events.EventTarget}. A listener can
<a name="line374"></a> * only be added once to an object.
<a name="line375"></a> *
<a name="line376"></a> * @param {EventTarget|goog.events.EventTarget} src The node to listen to
<a name="line377"></a> *     events on.
<a name="line378"></a> * @param {goog.events.EventWrapper} wrapper Event wrapper to use.
<a name="line379"></a> * @param {Function|Object} listener Callback method, or an object with a
<a name="line380"></a> *     handleEvent function.
<a name="line381"></a> * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
<a name="line382"></a> *     false).
<a name="line383"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line384"></a> */
<a name="line385"></a>goog.events.listenWithWrapper = function(src, wrapper, listener, opt_capt,
<a name="line386"></a>    opt_handler) {
<a name="line387"></a>  wrapper.listen(src, listener, opt_capt, opt_handler);
<a name="line388"></a>};
<a name="line389"></a>
<a name="line390"></a>
<a name="line391"></a>/**
<a name="line392"></a> * Removes an event listener which was added with listen().
<a name="line393"></a> *
<a name="line394"></a> * @param {goog.events.ListenableType} src The target to stop
<a name="line395"></a> *     listening to events on.
<a name="line396"></a> * @param {string|Array.&lt;string&gt;} type The name of the event without the &#39;on&#39;
<a name="line397"></a> *     prefix.
<a name="line398"></a> * @param {Function|Object} listener The listener function to remove.
<a name="line399"></a> * @param {boolean=} opt_capt In DOM-compliant browsers, this determines
<a name="line400"></a> *     whether the listener is fired during the capture or bubble phase of the
<a name="line401"></a> *     event.
<a name="line402"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line403"></a> * @return {?boolean} indicating whether the listener was there to remove.
<a name="line404"></a> */
<a name="line405"></a>goog.events.unlisten = function(src, type, listener, opt_capt, opt_handler) {
<a name="line406"></a>  if (goog.isArray(type)) {
<a name="line407"></a>    for (var i = 0; i &lt; type.length; i++) {
<a name="line408"></a>      goog.events.unlisten(src, type[i], listener, opt_capt, opt_handler);
<a name="line409"></a>    }
<a name="line410"></a>    return null;
<a name="line411"></a>  }
<a name="line412"></a>
<a name="line413"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line414"></a>      goog.events.Listenable.isImplementedBy(src)) {
<a name="line415"></a>    return src.unlisten(
<a name="line416"></a>        /** @type {string} */ (type),
<a name="line417"></a>        goog.events.wrapListener_(listener), opt_capt, opt_handler);
<a name="line418"></a>  }
<a name="line419"></a>
<a name="line420"></a>  var capture = !!opt_capt;
<a name="line421"></a>
<a name="line422"></a>  var listenerArray = goog.events.getListeners_(src, type, capture);
<a name="line423"></a>  if (!listenerArray) {
<a name="line424"></a>    return false;
<a name="line425"></a>  }
<a name="line426"></a>
<a name="line427"></a>  for (var i = 0; i &lt; listenerArray.length; i++) {
<a name="line428"></a>    if (listenerArray[i].listener == listener &amp;&amp;
<a name="line429"></a>        listenerArray[i].capture == capture &amp;&amp;
<a name="line430"></a>        listenerArray[i].handler == opt_handler) {
<a name="line431"></a>      return goog.events.unlistenByKey(listenerArray[i].key);
<a name="line432"></a>    }
<a name="line433"></a>  }
<a name="line434"></a>
<a name="line435"></a>  return false;
<a name="line436"></a>};
<a name="line437"></a>
<a name="line438"></a>
<a name="line439"></a>/**
<a name="line440"></a> * Removes an event listener which was added with listen() by the key
<a name="line441"></a> * returned by listen().
<a name="line442"></a> *
<a name="line443"></a> * @param {goog.events.Key} key The key returned by listen() for this
<a name="line444"></a> *     event listener.
<a name="line445"></a> * @return {boolean} indicating whether the listener was there to remove.
<a name="line446"></a> */
<a name="line447"></a>goog.events.unlistenByKey = function(key) {
<a name="line448"></a>  // TODO(user): When we flip goog.events.Key to be ListenableKey,
<a name="line449"></a>  // we need to change this.
<a name="line450"></a>  var listener = goog.events.listeners_[key];
<a name="line451"></a>  if (!listener) {
<a name="line452"></a>    return false;
<a name="line453"></a>  }
<a name="line454"></a>  if (listener.removed) {
<a name="line455"></a>    return false;
<a name="line456"></a>  }
<a name="line457"></a>
<a name="line458"></a>  var src = listener.src;
<a name="line459"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line460"></a>      goog.events.Listenable.isImplementedBy(src)) {
<a name="line461"></a>    return src.unlistenByKey(listener);
<a name="line462"></a>  }
<a name="line463"></a>
<a name="line464"></a>  var type = listener.type;
<a name="line465"></a>  var proxy = listener.proxy;
<a name="line466"></a>  var capture = listener.capture;
<a name="line467"></a>
<a name="line468"></a>  if (src.removeEventListener) {
<a name="line469"></a>    // EventTarget calls unlisten so we need to ensure that the source is not
<a name="line470"></a>    // an event target to prevent re-entry.
<a name="line471"></a>    // TODO(arv): What is this goog.global for? Why would anyone listen to
<a name="line472"></a>    // events on the [[Global]] object? Is it supposed to be window? Why would
<a name="line473"></a>    // we not want to allow removing event listeners on the window?
<a name="line474"></a>    if (src == goog.global || !src.customEvent_) {
<a name="line475"></a>      src.removeEventListener(type, proxy, capture);
<a name="line476"></a>    }
<a name="line477"></a>  } else if (src.detachEvent) {
<a name="line478"></a>    src.detachEvent(goog.events.getOnString_(type), proxy);
<a name="line479"></a>  }
<a name="line480"></a>
<a name="line481"></a>  var srcUid = goog.getUid(src);
<a name="line482"></a>
<a name="line483"></a>  // In a perfect implementation we would decrement the remaining_ field here
<a name="line484"></a>  // but then we would need to know if the listener has already been fired or
<a name="line485"></a>  // not. We therefore skip doing this and in this uncommon case the entire
<a name="line486"></a>  // ancestor chain will need to be traversed as before.
<a name="line487"></a>
<a name="line488"></a>  // Remove from sources_
<a name="line489"></a>  if (goog.events.sources_[srcUid]) {
<a name="line490"></a>    var sourcesArray = goog.events.sources_[srcUid];
<a name="line491"></a>    goog.array.remove(sourcesArray, listener);
<a name="line492"></a>    if (sourcesArray.length == 0) {
<a name="line493"></a>      delete goog.events.sources_[srcUid];
<a name="line494"></a>    }
<a name="line495"></a>  }
<a name="line496"></a>
<a name="line497"></a>  listener.removed = true;
<a name="line498"></a>
<a name="line499"></a>  // There are some esoteric situations where the hash code of an object
<a name="line500"></a>  // can change, and we won&#39;t be able to find the listenerArray anymore.
<a name="line501"></a>  // For example, if you&#39;re listening on a window, and the user navigates to
<a name="line502"></a>  // a different window, the UID will disappear.
<a name="line503"></a>  //
<a name="line504"></a>  // It should be impossible to ever find the original listenerArray, so it
<a name="line505"></a>  // doesn&#39;t really matter if we can&#39;t clean it up in this case.
<a name="line506"></a>  var listenerArray = goog.events.listenerTree_[type][capture][srcUid];
<a name="line507"></a>  if (listenerArray) {
<a name="line508"></a>    listenerArray.needsCleanup_ = true;
<a name="line509"></a>    goog.events.cleanUp_(type, capture, srcUid, listenerArray);
<a name="line510"></a>  }
<a name="line511"></a>
<a name="line512"></a>  delete goog.events.listeners_[key];
<a name="line513"></a>
<a name="line514"></a>  return true;
<a name="line515"></a>};
<a name="line516"></a>
<a name="line517"></a>
<a name="line518"></a>/**
<a name="line519"></a> * Removes an event listener which was added with listenWithWrapper().
<a name="line520"></a> *
<a name="line521"></a> * @param {EventTarget|goog.events.EventTarget} src The target to stop
<a name="line522"></a> *     listening to events on.
<a name="line523"></a> * @param {goog.events.EventWrapper} wrapper Event wrapper to use.
<a name="line524"></a> * @param {Function|Object} listener The listener function to remove.
<a name="line525"></a> * @param {boolean=} opt_capt In DOM-compliant browsers, this determines
<a name="line526"></a> *     whether the listener is fired during the capture or bubble phase of the
<a name="line527"></a> *     event.
<a name="line528"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line529"></a> */
<a name="line530"></a>goog.events.unlistenWithWrapper = function(src, wrapper, listener, opt_capt,
<a name="line531"></a>    opt_handler) {
<a name="line532"></a>  wrapper.unlisten(src, listener, opt_capt, opt_handler);
<a name="line533"></a>};
<a name="line534"></a>
<a name="line535"></a>
<a name="line536"></a>/**
<a name="line537"></a> * Cleans up goog.events internal data structure. This should be
<a name="line538"></a> * called by all implementations of goog.events.Listenable when
<a name="line539"></a> * removing listeners.
<a name="line540"></a> *
<a name="line541"></a> * TODO(user): Once we remove numeric key support from
<a name="line542"></a> * goog.events.listen and friend, we will be able to remove this
<a name="line543"></a> * requirement.
<a name="line544"></a> *
<a name="line545"></a> * @param {goog.events.ListenableKey} listenableKey The key to clean up.
<a name="line546"></a> */
<a name="line547"></a>goog.events.cleanUp = function(listenableKey) {
<a name="line548"></a>  delete goog.events.listeners_[listenableKey.key];
<a name="line549"></a>};
<a name="line550"></a>
<a name="line551"></a>
<a name="line552"></a>/**
<a name="line553"></a> * Cleans up the listener array as well as the listener tree
<a name="line554"></a> * @param {string} type  The type of the event.
<a name="line555"></a> * @param {boolean} capture Whether to clean up capture phase listeners instead
<a name="line556"></a> *     bubble phase listeners.
<a name="line557"></a> * @param {number} srcUid  The unique ID of the source.
<a name="line558"></a> * @param {Array.&lt;goog.events.Listener&gt;} listenerArray The array being cleaned.
<a name="line559"></a> * @private
<a name="line560"></a> */
<a name="line561"></a>goog.events.cleanUp_ = function(type, capture, srcUid, listenerArray) {
<a name="line562"></a>  // The listener array gets locked during the dispatch phase so that removals
<a name="line563"></a>  // of listeners during this phase does not screw up the indeces. This method
<a name="line564"></a>  // is called after we have removed a listener as well as after the dispatch
<a name="line565"></a>  // phase in case any listeners were removed.
<a name="line566"></a>  if (!listenerArray.locked_) { // catches both 0 and not set
<a name="line567"></a>    if (listenerArray.needsCleanup_) {
<a name="line568"></a>      // Loop over the listener array and remove listeners that have removed set
<a name="line569"></a>      // to true. This could have been done with filter or something similar but
<a name="line570"></a>      // we want to change the array in place and we want to minimize
<a name="line571"></a>      // allocations. Adding a listener during this phase adds to the end of the
<a name="line572"></a>      // array so that works fine as long as the length is rechecked every in
<a name="line573"></a>      // iteration.
<a name="line574"></a>      for (var oldIndex = 0, newIndex = 0;
<a name="line575"></a>           oldIndex &lt; listenerArray.length;
<a name="line576"></a>           oldIndex++) {
<a name="line577"></a>        if (listenerArray[oldIndex].removed) {
<a name="line578"></a>          var proxy = listenerArray[oldIndex].proxy;
<a name="line579"></a>          proxy.src = null;
<a name="line580"></a>          continue;
<a name="line581"></a>        }
<a name="line582"></a>        if (oldIndex != newIndex) {
<a name="line583"></a>          listenerArray[newIndex] = listenerArray[oldIndex];
<a name="line584"></a>        }
<a name="line585"></a>        newIndex++;
<a name="line586"></a>      }
<a name="line587"></a>      listenerArray.length = newIndex;
<a name="line588"></a>
<a name="line589"></a>      listenerArray.needsCleanup_ = false;
<a name="line590"></a>
<a name="line591"></a>      // In case the length is now zero we release the object.
<a name="line592"></a>      if (newIndex == 0) {
<a name="line593"></a>        delete goog.events.listenerTree_[type][capture][srcUid];
<a name="line594"></a>        goog.events.listenerTree_[type][capture].count_--;
<a name="line595"></a>
<a name="line596"></a>        if (goog.events.listenerTree_[type][capture].count_ == 0) {
<a name="line597"></a>          delete goog.events.listenerTree_[type][capture];
<a name="line598"></a>          goog.events.listenerTree_[type].count_--;
<a name="line599"></a>        }
<a name="line600"></a>
<a name="line601"></a>        if (goog.events.listenerTree_[type].count_ == 0) {
<a name="line602"></a>          delete goog.events.listenerTree_[type];
<a name="line603"></a>        }
<a name="line604"></a>      }
<a name="line605"></a>
<a name="line606"></a>    }
<a name="line607"></a>  }
<a name="line608"></a>};
<a name="line609"></a>
<a name="line610"></a>
<a name="line611"></a>/**
<a name="line612"></a> * Removes all listeners from an object, if no object is specified it will
<a name="line613"></a> * remove all listeners that have been registered.  You can also optionally
<a name="line614"></a> * remove listeners of a particular type or capture phase.
<a name="line615"></a> *
<a name="line616"></a> * removeAll() will not remove listeners registered directly on a
<a name="line617"></a> * goog.events.Listenable and listeners registered via add(Once)Listener.
<a name="line618"></a> *
<a name="line619"></a> * @param {Object=} opt_obj Object to remove listeners from.
<a name="line620"></a> * @param {string=} opt_type Type of event to, default is all types.
<a name="line621"></a> * @return {number} Number of listeners removed.
<a name="line622"></a> */
<a name="line623"></a>goog.events.removeAll = function(opt_obj, opt_type) {
<a name="line624"></a>  var count = 0;
<a name="line625"></a>
<a name="line626"></a>  var noObj = opt_obj == null;
<a name="line627"></a>  var noType = opt_type == null;
<a name="line628"></a>
<a name="line629"></a>  if (!noObj) {
<a name="line630"></a>    if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line631"></a>        opt_obj &amp;&amp; goog.events.Listenable.isImplementedBy(opt_obj)) {
<a name="line632"></a>      return opt_obj.removeAllListeners(opt_type);
<a name="line633"></a>    }
<a name="line634"></a>
<a name="line635"></a>    var srcUid = goog.getUid(/** @type {Object} */ (opt_obj));
<a name="line636"></a>    if (goog.events.sources_[srcUid]) {
<a name="line637"></a>      var sourcesArray = goog.events.sources_[srcUid];
<a name="line638"></a>      for (var i = sourcesArray.length - 1; i &gt;= 0; i--) {
<a name="line639"></a>        var listener = sourcesArray[i];
<a name="line640"></a>        if (noType || opt_type == listener.type) {
<a name="line641"></a>          goog.events.unlistenByKey(listener.key);
<a name="line642"></a>          count++;
<a name="line643"></a>        }
<a name="line644"></a>      }
<a name="line645"></a>    }
<a name="line646"></a>  } else {
<a name="line647"></a>    goog.object.forEach(goog.events.listeners_, function(listener, key) {
<a name="line648"></a>      goog.events.unlistenByKey(key);
<a name="line649"></a>      count++;
<a name="line650"></a>    });
<a name="line651"></a>  }
<a name="line652"></a>
<a name="line653"></a>  return count;
<a name="line654"></a>};
<a name="line655"></a>
<a name="line656"></a>
<a name="line657"></a>/**
<a name="line658"></a> * Gets the listeners for a given object, type and capture phase.
<a name="line659"></a> *
<a name="line660"></a> * @param {Object} obj Object to get listeners for.
<a name="line661"></a> * @param {string} type Event type.
<a name="line662"></a> * @param {boolean} capture Capture phase?.
<a name="line663"></a> * @return {Array.&lt;goog.events.Listener&gt;} Array of listener objects.
<a name="line664"></a> */
<a name="line665"></a>goog.events.getListeners = function(obj, type, capture) {
<a name="line666"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line667"></a>      goog.events.Listenable.isImplementedBy(obj)) {
<a name="line668"></a>    return obj.getListeners(type, capture);
<a name="line669"></a>  } else {
<a name="line670"></a>    return goog.events.getListeners_(obj, type, capture) || [];
<a name="line671"></a>  }
<a name="line672"></a>};
<a name="line673"></a>
<a name="line674"></a>
<a name="line675"></a>/**
<a name="line676"></a> * Gets the listeners for a given object, type and capture phase.
<a name="line677"></a> *
<a name="line678"></a> * @param {Object} obj Object to get listeners for.
<a name="line679"></a> * @param {?string} type Event type.
<a name="line680"></a> * @param {boolean} capture Capture phase?.
<a name="line681"></a> * @return {Array.&lt;goog.events.Listener&gt;?} Array of listener objects.
<a name="line682"></a> *     Returns null if object has no listeners of that type.
<a name="line683"></a> * @private
<a name="line684"></a> */
<a name="line685"></a>goog.events.getListeners_ = function(obj, type, capture) {
<a name="line686"></a>  var map = goog.events.listenerTree_;
<a name="line687"></a>  if (type in map) {
<a name="line688"></a>    map = map[type];
<a name="line689"></a>    if (capture in map) {
<a name="line690"></a>      map = map[capture];
<a name="line691"></a>      var objUid = goog.getUid(obj);
<a name="line692"></a>      if (map[objUid]) {
<a name="line693"></a>        return map[objUid];
<a name="line694"></a>      }
<a name="line695"></a>    }
<a name="line696"></a>  }
<a name="line697"></a>
<a name="line698"></a>  return null;
<a name="line699"></a>};
<a name="line700"></a>
<a name="line701"></a>
<a name="line702"></a>/**
<a name="line703"></a> * Gets the goog.events.Listener for the event or null if no such listener is
<a name="line704"></a> * in use.
<a name="line705"></a> *
<a name="line706"></a> * @param {EventTarget|goog.events.EventTarget} src The node from which to get
<a name="line707"></a> *     listeners.
<a name="line708"></a> * @param {?string} type The name of the event without the &#39;on&#39; prefix.
<a name="line709"></a> * @param {Function|Object} listener The listener function to get.
<a name="line710"></a> * @param {boolean=} opt_capt In DOM-compliant browsers, this determines
<a name="line711"></a> *                            whether the listener is fired during the
<a name="line712"></a> *                            capture or bubble phase of the event.
<a name="line713"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line714"></a> * @return {goog.events.ListenableKey} the found listener or null if not found.
<a name="line715"></a> */
<a name="line716"></a>goog.events.getListener = function(src, type, listener, opt_capt, opt_handler) {
<a name="line717"></a>  var capture = !!opt_capt;
<a name="line718"></a>
<a name="line719"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line720"></a>      goog.events.Listenable.isImplementedBy(src)) {
<a name="line721"></a>    return src.getListener(
<a name="line722"></a>        /** @type {string} */ (type),
<a name="line723"></a>        goog.events.wrapListener_(listener), capture, opt_handler);
<a name="line724"></a>  }
<a name="line725"></a>
<a name="line726"></a>  var listenerArray = goog.events.getListeners_(src, type, capture);
<a name="line727"></a>  if (listenerArray) {
<a name="line728"></a>    for (var i = 0; i &lt; listenerArray.length; i++) {
<a name="line729"></a>      // If goog.events.unlistenByKey is called during an event dispatch
<a name="line730"></a>      // then the listener array won&#39;t get cleaned up and there might be
<a name="line731"></a>      // &#39;removed&#39; listeners in the list. Ignore those.
<a name="line732"></a>      if (!listenerArray[i].removed &amp;&amp;
<a name="line733"></a>          listenerArray[i].listener == listener &amp;&amp;
<a name="line734"></a>          listenerArray[i].capture == capture &amp;&amp;
<a name="line735"></a>          listenerArray[i].handler == opt_handler) {
<a name="line736"></a>        // We already have this listener. Return its key.
<a name="line737"></a>        return listenerArray[i];
<a name="line738"></a>      }
<a name="line739"></a>    }
<a name="line740"></a>  }
<a name="line741"></a>  return null;
<a name="line742"></a>};
<a name="line743"></a>
<a name="line744"></a>
<a name="line745"></a>/**
<a name="line746"></a> * Returns whether an event target has any active listeners matching the
<a name="line747"></a> * specified signature. If either the type or capture parameters are
<a name="line748"></a> * unspecified, the function will match on the remaining criteria.
<a name="line749"></a> *
<a name="line750"></a> * @param {EventTarget|goog.events.EventTarget} obj Target to get listeners for.
<a name="line751"></a> * @param {string=} opt_type Event type.
<a name="line752"></a> * @param {boolean=} opt_capture Whether to check for capture or bubble-phase
<a name="line753"></a> *     listeners.
<a name="line754"></a> * @return {boolean} Whether an event target has one or more listeners matching
<a name="line755"></a> *     the requested type and/or capture phase.
<a name="line756"></a> */
<a name="line757"></a>goog.events.hasListener = function(obj, opt_type, opt_capture) {
<a name="line758"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line759"></a>      goog.events.Listenable.isImplementedBy(obj)) {
<a name="line760"></a>    return obj.hasListener(opt_type, opt_capture);
<a name="line761"></a>  }
<a name="line762"></a>
<a name="line763"></a>  var objUid = goog.getUid(obj);
<a name="line764"></a>  var listeners = goog.events.sources_[objUid];
<a name="line765"></a>
<a name="line766"></a>  if (listeners) {
<a name="line767"></a>    var hasType = goog.isDef(opt_type);
<a name="line768"></a>    var hasCapture = goog.isDef(opt_capture);
<a name="line769"></a>
<a name="line770"></a>    if (hasType &amp;&amp; hasCapture) {
<a name="line771"></a>      // Lookup in the listener tree whether the specified listener exists.
<a name="line772"></a>      var map = goog.events.listenerTree_[opt_type];
<a name="line773"></a>      return !!map &amp;&amp; !!map[opt_capture] &amp;&amp; objUid in map[opt_capture];
<a name="line774"></a>
<a name="line775"></a>    } else if (!(hasType || hasCapture)) {
<a name="line776"></a>      // Simple check for whether the event target has any listeners at all.
<a name="line777"></a>      return true;
<a name="line778"></a>
<a name="line779"></a>    } else {
<a name="line780"></a>      // Iterate through the listeners for the event target to find a match.
<a name="line781"></a>      return goog.array.some(listeners, function(listener) {
<a name="line782"></a>        return (hasType &amp;&amp; listener.type == opt_type) ||
<a name="line783"></a>               (hasCapture &amp;&amp; listener.capture == opt_capture);
<a name="line784"></a>      });
<a name="line785"></a>    }
<a name="line786"></a>  }
<a name="line787"></a>
<a name="line788"></a>  return false;
<a name="line789"></a>};
<a name="line790"></a>
<a name="line791"></a>
<a name="line792"></a>/**
<a name="line793"></a> * Provides a nice string showing the normalized event objects public members
<a name="line794"></a> * @param {Object} e Event Object.
<a name="line795"></a> * @return {string} String of the public members of the normalized event object.
<a name="line796"></a> */
<a name="line797"></a>goog.events.expose = function(e) {
<a name="line798"></a>  var str = [];
<a name="line799"></a>  for (var key in e) {
<a name="line800"></a>    if (e[key] &amp;&amp; e[key].id) {
<a name="line801"></a>      str.push(key + &#39; = &#39; + e[key] + &#39; (&#39; + e[key].id + &#39;)&#39;);
<a name="line802"></a>    } else {
<a name="line803"></a>      str.push(key + &#39; = &#39; + e[key]);
<a name="line804"></a>    }
<a name="line805"></a>  }
<a name="line806"></a>  return str.join(&#39;\n&#39;);
<a name="line807"></a>};
<a name="line808"></a>
<a name="line809"></a>
<a name="line810"></a>/**
<a name="line811"></a> * Returns a string wth on prepended to the specified type. This is used for IE
<a name="line812"></a> * which expects &quot;on&quot; to be prepended. This function caches the string in order
<a name="line813"></a> * to avoid extra allocations in steady state.
<a name="line814"></a> * @param {string} type Event type strng.
<a name="line815"></a> * @return {string} The type string with &#39;on&#39; prepended.
<a name="line816"></a> * @private
<a name="line817"></a> */
<a name="line818"></a>goog.events.getOnString_ = function(type) {
<a name="line819"></a>  if (type in goog.events.onStringMap_) {
<a name="line820"></a>    return goog.events.onStringMap_[type];
<a name="line821"></a>  }
<a name="line822"></a>  return goog.events.onStringMap_[type] = goog.events.onString_ + type;
<a name="line823"></a>};
<a name="line824"></a>
<a name="line825"></a>
<a name="line826"></a>/**
<a name="line827"></a> * Fires an object&#39;s listeners of a particular type and phase
<a name="line828"></a> *
<a name="line829"></a> * @param {Object} obj Object whose listeners to call.
<a name="line830"></a> * @param {string} type Event type.
<a name="line831"></a> * @param {boolean} capture Which event phase.
<a name="line832"></a> * @param {Object} eventObject Event object to be passed to listener.
<a name="line833"></a> * @return {boolean} True if all listeners returned true else false.
<a name="line834"></a> */
<a name="line835"></a>goog.events.fireListeners = function(obj, type, capture, eventObject) {
<a name="line836"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE &amp;&amp;
<a name="line837"></a>      goog.events.Listenable.isImplementedBy(obj)) {
<a name="line838"></a>    return obj.fireListeners(type, capture, eventObject);
<a name="line839"></a>  }
<a name="line840"></a>
<a name="line841"></a>  var map = goog.events.listenerTree_;
<a name="line842"></a>  if (type in map) {
<a name="line843"></a>    map = map[type];
<a name="line844"></a>    if (capture in map) {
<a name="line845"></a>      return goog.events.fireListeners_(map[capture], obj, type,
<a name="line846"></a>                                        capture, eventObject);
<a name="line847"></a>    }
<a name="line848"></a>  }
<a name="line849"></a>  return true;
<a name="line850"></a>};
<a name="line851"></a>
<a name="line852"></a>
<a name="line853"></a>/**
<a name="line854"></a> * Fires an object&#39;s listeners of a particular type and phase.
<a name="line855"></a> *
<a name="line856"></a> * @param {Object} map Object with listeners in it.
<a name="line857"></a> * @param {Object} obj Object whose listeners to call.
<a name="line858"></a> * @param {string} type Event type.
<a name="line859"></a> * @param {boolean} capture Which event phase.
<a name="line860"></a> * @param {Object} eventObject Event object to be passed to listener.
<a name="line861"></a> * @return {boolean} True if all listeners returned true else false.
<a name="line862"></a> * @private
<a name="line863"></a> */
<a name="line864"></a>goog.events.fireListeners_ = function(map, obj, type, capture, eventObject) {
<a name="line865"></a>  var retval = 1;
<a name="line866"></a>
<a name="line867"></a>  var objUid = goog.getUid(obj);
<a name="line868"></a>  if (map[objUid]) {
<a name="line869"></a>    var remaining = --map.remaining_;
<a name="line870"></a>    var listenerArray = map[objUid];
<a name="line871"></a>
<a name="line872"></a>    // If locked_ is not set (and if already 0) initialize it to 1.
<a name="line873"></a>    if (!listenerArray.locked_) {
<a name="line874"></a>      listenerArray.locked_ = 1;
<a name="line875"></a>    } else {
<a name="line876"></a>      listenerArray.locked_++;
<a name="line877"></a>    }
<a name="line878"></a>
<a name="line879"></a>    try {
<a name="line880"></a>      // Events added in the dispatch phase should not be dispatched in
<a name="line881"></a>      // the current dispatch phase. They will be included in the next
<a name="line882"></a>      // dispatch phase though.
<a name="line883"></a>      var length = listenerArray.length;
<a name="line884"></a>      for (var i = 0; i &lt; length; i++) {
<a name="line885"></a>        var listener = listenerArray[i];
<a name="line886"></a>        // We might not have a listener if the listener was removed.
<a name="line887"></a>        if (listener &amp;&amp; !listener.removed) {
<a name="line888"></a>          retval &amp;=
<a name="line889"></a>              goog.events.fireListener(listener, eventObject) !== false;
<a name="line890"></a>        }
<a name="line891"></a>      }
<a name="line892"></a>    } finally {
<a name="line893"></a>      // Allow the count of targets remaining to increase (if perhaps we have
<a name="line894"></a>      // added listeners) but do not allow it to decrease if we have reentered
<a name="line895"></a>      // this method through a listener dispatching the same event type,
<a name="line896"></a>      // resetting and exhausted the remaining count.
<a name="line897"></a>      map.remaining_ = Math.max(remaining, map.remaining_);
<a name="line898"></a>      listenerArray.locked_--;
<a name="line899"></a>      goog.events.cleanUp_(type, capture, objUid, listenerArray);
<a name="line900"></a>    }
<a name="line901"></a>  }
<a name="line902"></a>
<a name="line903"></a>  return Boolean(retval);
<a name="line904"></a>};
<a name="line905"></a>
<a name="line906"></a>
<a name="line907"></a>/**
<a name="line908"></a> * Fires a listener with a set of arguments
<a name="line909"></a> *
<a name="line910"></a> * @param {goog.events.Listener} listener The listener object to call.
<a name="line911"></a> * @param {Object} eventObject The event object to pass to the listener.
<a name="line912"></a> * @return {boolean} Result of listener.
<a name="line913"></a> */
<a name="line914"></a>goog.events.fireListener = function(listener, eventObject) {
<a name="line915"></a>  if (listener.callOnce) {
<a name="line916"></a>    goog.events.unlistenByKey(listener.key);
<a name="line917"></a>  }
<a name="line918"></a>  return listener.handleEvent(eventObject);
<a name="line919"></a>};
<a name="line920"></a>
<a name="line921"></a>
<a name="line922"></a>/**
<a name="line923"></a> * Gets the total number of listeners currently in the system.
<a name="line924"></a> * @return {number} Number of listeners.
<a name="line925"></a> */
<a name="line926"></a>goog.events.getTotalListenerCount = function() {
<a name="line927"></a>  return goog.object.getCount(goog.events.listeners_);
<a name="line928"></a>};
<a name="line929"></a>
<a name="line930"></a>
<a name="line931"></a>/**
<a name="line932"></a> * Dispatches an event (or event like object) and calls all listeners
<a name="line933"></a> * listening for events of this type. The type of the event is decided by the
<a name="line934"></a> * type property on the event object.
<a name="line935"></a> *
<a name="line936"></a> * If any of the listeners returns false OR calls preventDefault then this
<a name="line937"></a> * function will return false.  If one of the capture listeners calls
<a name="line938"></a> * stopPropagation, then the bubble listeners won&#39;t fire.
<a name="line939"></a> *
<a name="line940"></a> * @param {goog.events.Listenable|goog.events.EventTarget} src The
<a name="line941"></a> *     event target.
<a name="line942"></a> * @param {goog.events.EventLike} e Event object.
<a name="line943"></a> * @return {boolean} If anyone called preventDefault on the event object (or
<a name="line944"></a> *     if any of the handlers returns false) this will also return false.
<a name="line945"></a> *     If there are no handlers, or if all handlers return true, this returns
<a name="line946"></a> *     true.
<a name="line947"></a> */
<a name="line948"></a>goog.events.dispatchEvent = function(src, e) {
<a name="line949"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line950"></a>    return src.dispatchEvent(e);
<a name="line951"></a>  }
<a name="line952"></a>
<a name="line953"></a>  var type = e.type || e;
<a name="line954"></a>  var map = goog.events.listenerTree_;
<a name="line955"></a>  if (!(type in map)) {
<a name="line956"></a>    return true;
<a name="line957"></a>  }
<a name="line958"></a>
<a name="line959"></a>  // If accepting a string or object, create a custom event object so that
<a name="line960"></a>  // preventDefault and stopPropagation work with the event.
<a name="line961"></a>  if (goog.isString(e)) {
<a name="line962"></a>    e = new goog.events.Event(e, src);
<a name="line963"></a>  } else if (!(e instanceof goog.events.Event)) {
<a name="line964"></a>    var oldEvent = e;
<a name="line965"></a>    e = new goog.events.Event(/** @type {string} */ (type), src);
<a name="line966"></a>    goog.object.extend(e, oldEvent);
<a name="line967"></a>  } else {
<a name="line968"></a>    e.target = e.target || src;
<a name="line969"></a>  }
<a name="line970"></a>
<a name="line971"></a>  var rv = 1, ancestors;
<a name="line972"></a>
<a name="line973"></a>  map = map[type];
<a name="line974"></a>  var hasCapture = true in map;
<a name="line975"></a>  var targetsMap;
<a name="line976"></a>
<a name="line977"></a>  if (hasCapture) {
<a name="line978"></a>    // Build ancestors now
<a name="line979"></a>    ancestors = [];
<a name="line980"></a>    for (var parent = src; parent; parent = parent.getParentEventTarget()) {
<a name="line981"></a>      ancestors.push(parent);
<a name="line982"></a>    }
<a name="line983"></a>
<a name="line984"></a>    targetsMap = map[true];
<a name="line985"></a>    targetsMap.remaining_ = targetsMap.count_;
<a name="line986"></a>
<a name="line987"></a>    // Call capture listeners
<a name="line988"></a>    for (var i = ancestors.length - 1;
<a name="line989"></a>         !e.propagationStopped_ &amp;&amp; i &gt;= 0 &amp;&amp; targetsMap.remaining_;
<a name="line990"></a>         i--) {
<a name="line991"></a>      e.currentTarget = ancestors[i];
<a name="line992"></a>      rv &amp;= goog.events.fireListeners_(targetsMap, ancestors[i], e.type,
<a name="line993"></a>                                       true, e) &amp;&amp;
<a name="line994"></a>            e.returnValue_ != false;
<a name="line995"></a>    }
<a name="line996"></a>  }
<a name="line997"></a>
<a name="line998"></a>  var hasBubble = false in map;
<a name="line999"></a>  if (hasBubble) {
<a name="line1000"></a>    targetsMap = map[false];
<a name="line1001"></a>    targetsMap.remaining_ = targetsMap.count_;
<a name="line1002"></a>
<a name="line1003"></a>    if (hasCapture) { // We have the ancestors.
<a name="line1004"></a>
<a name="line1005"></a>      // Call bubble listeners
<a name="line1006"></a>      for (var i = 0; !e.propagationStopped_ &amp;&amp; i &lt; ancestors.length &amp;&amp;
<a name="line1007"></a>           targetsMap.remaining_;
<a name="line1008"></a>           i++) {
<a name="line1009"></a>        e.currentTarget = ancestors[i];
<a name="line1010"></a>        rv &amp;= goog.events.fireListeners_(targetsMap, ancestors[i], e.type,
<a name="line1011"></a>                                         false, e) &amp;&amp;
<a name="line1012"></a>              e.returnValue_ != false;
<a name="line1013"></a>      }
<a name="line1014"></a>    } else {
<a name="line1015"></a>      // In case we don&#39;t have capture we don&#39;t have to build up the
<a name="line1016"></a>      // ancestors array.
<a name="line1017"></a>
<a name="line1018"></a>      for (var current = src;
<a name="line1019"></a>           !e.propagationStopped_ &amp;&amp; current &amp;&amp; targetsMap.remaining_;
<a name="line1020"></a>           current = current.getParentEventTarget()) {
<a name="line1021"></a>        e.currentTarget = current;
<a name="line1022"></a>        rv &amp;= goog.events.fireListeners_(targetsMap, current, e.type,
<a name="line1023"></a>                                         false, e) &amp;&amp;
<a name="line1024"></a>              e.returnValue_ != false;
<a name="line1025"></a>      }
<a name="line1026"></a>    }
<a name="line1027"></a>  }
<a name="line1028"></a>
<a name="line1029"></a>  return Boolean(rv);
<a name="line1030"></a>};
<a name="line1031"></a>
<a name="line1032"></a>
<a name="line1033"></a>/**
<a name="line1034"></a> * Installs exception protection for the browser event entry point using the
<a name="line1035"></a> * given error handler.
<a name="line1036"></a> *
<a name="line1037"></a> * @param {goog.debug.ErrorHandler} errorHandler Error handler with which to
<a name="line1038"></a> *     protect the entry point.
<a name="line1039"></a> */
<a name="line1040"></a>goog.events.protectBrowserEventEntryPoint = function(errorHandler) {
<a name="line1041"></a>  goog.events.handleBrowserEvent_ = errorHandler.protectEntryPoint(
<a name="line1042"></a>      goog.events.handleBrowserEvent_);
<a name="line1043"></a>};
<a name="line1044"></a>
<a name="line1045"></a>
<a name="line1046"></a>/**
<a name="line1047"></a> * Handles an event and dispatches it to the correct listeners. This
<a name="line1048"></a> * function is a proxy for the real listener the user specified.
<a name="line1049"></a> *
<a name="line1050"></a> * @param {goog.events.Listener} listener The listener object.
<a name="line1051"></a> * @param {Event=} opt_evt Optional event object that gets passed in via the
<a name="line1052"></a> *     native event handlers.
<a name="line1053"></a> * @return {boolean} Result of the event handler.
<a name="line1054"></a> * @this {goog.events.EventTarget|Object} The object or Element that
<a name="line1055"></a> *     fired the event.
<a name="line1056"></a> * @private
<a name="line1057"></a> */
<a name="line1058"></a>goog.events.handleBrowserEvent_ = function(listener, opt_evt) {
<a name="line1059"></a>  if (listener.removed) {
<a name="line1060"></a>    return true;
<a name="line1061"></a>  }
<a name="line1062"></a>
<a name="line1063"></a>  var type = listener.type;
<a name="line1064"></a>  var map = goog.events.listenerTree_;
<a name="line1065"></a>
<a name="line1066"></a>  if (!(type in map)) {
<a name="line1067"></a>    return true;
<a name="line1068"></a>  }
<a name="line1069"></a>  map = map[type];
<a name="line1070"></a>  var retval, targetsMap;
<a name="line1071"></a>  // Synthesize event propagation if the browser does not support W3C
<a name="line1072"></a>  // event model.
<a name="line1073"></a>  if (!goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT) {
<a name="line1074"></a>    var ieEvent = opt_evt ||
<a name="line1075"></a>        /** @type {Event} */ (goog.getObjectByName(&#39;window.event&#39;));
<a name="line1076"></a>
<a name="line1077"></a>    // Check if we have any capturing event listeners for this type.
<a name="line1078"></a>    var hasCapture = true in map;
<a name="line1079"></a>    var hasBubble = false in map;
<a name="line1080"></a>
<a name="line1081"></a>    if (hasCapture) {
<a name="line1082"></a>      if (goog.events.isMarkedIeEvent_(ieEvent)) {
<a name="line1083"></a>        return true;
<a name="line1084"></a>      }
<a name="line1085"></a>
<a name="line1086"></a>      goog.events.markIeEvent_(ieEvent);
<a name="line1087"></a>    }
<a name="line1088"></a>
<a name="line1089"></a>    var evt = new goog.events.BrowserEvent();
<a name="line1090"></a>    // TODO(user): update @this for this function
<a name="line1091"></a>    evt.init(ieEvent, /** @type {EventTarget} */ (this));
<a name="line1092"></a>
<a name="line1093"></a>    retval = true;
<a name="line1094"></a>    try {
<a name="line1095"></a>      if (hasCapture) {
<a name="line1096"></a>        var ancestors = [];
<a name="line1097"></a>
<a name="line1098"></a>        for (var parent = evt.currentTarget;
<a name="line1099"></a>             parent;
<a name="line1100"></a>             parent = parent.parentNode) {
<a name="line1101"></a>          ancestors.push(parent);
<a name="line1102"></a>        }
<a name="line1103"></a>
<a name="line1104"></a>        targetsMap = map[true];
<a name="line1105"></a>        targetsMap.remaining_ = targetsMap.count_;
<a name="line1106"></a>
<a name="line1107"></a>        // Call capture listeners
<a name="line1108"></a>        for (var i = ancestors.length - 1;
<a name="line1109"></a>             !evt.propagationStopped_ &amp;&amp; i &gt;= 0 &amp;&amp; targetsMap.remaining_;
<a name="line1110"></a>             i--) {
<a name="line1111"></a>          evt.currentTarget = ancestors[i];
<a name="line1112"></a>          retval &amp;= goog.events.fireListeners_(targetsMap, ancestors[i], type,
<a name="line1113"></a>                                               true, evt);
<a name="line1114"></a>        }
<a name="line1115"></a>
<a name="line1116"></a>        if (hasBubble) {
<a name="line1117"></a>          targetsMap = map[false];
<a name="line1118"></a>          targetsMap.remaining_ = targetsMap.count_;
<a name="line1119"></a>
<a name="line1120"></a>          // Call bubble listeners
<a name="line1121"></a>          for (var i = 0;
<a name="line1122"></a>               !evt.propagationStopped_ &amp;&amp; i &lt; ancestors.length &amp;&amp;
<a name="line1123"></a>               targetsMap.remaining_;
<a name="line1124"></a>               i++) {
<a name="line1125"></a>            evt.currentTarget = ancestors[i];
<a name="line1126"></a>            retval &amp;= goog.events.fireListeners_(targetsMap, ancestors[i], type,
<a name="line1127"></a>                                                 false, evt);
<a name="line1128"></a>          }
<a name="line1129"></a>        }
<a name="line1130"></a>
<a name="line1131"></a>      } else {
<a name="line1132"></a>        // Bubbling, let IE handle the propagation.
<a name="line1133"></a>        retval = goog.events.fireListener(listener, evt);
<a name="line1134"></a>      }
<a name="line1135"></a>
<a name="line1136"></a>    } finally {
<a name="line1137"></a>      if (ancestors) {
<a name="line1138"></a>        ancestors.length = 0;
<a name="line1139"></a>      }
<a name="line1140"></a>    }
<a name="line1141"></a>    return retval;
<a name="line1142"></a>  } // IE
<a name="line1143"></a>
<a name="line1144"></a>  // Caught a non-IE DOM event. 1 additional argument which is the event object
<a name="line1145"></a>  var be = new goog.events.BrowserEvent(
<a name="line1146"></a>      opt_evt, /** @type {EventTarget} */ (this));
<a name="line1147"></a>  retval = goog.events.fireListener(listener, be);
<a name="line1148"></a>  return retval;
<a name="line1149"></a>};
<a name="line1150"></a>
<a name="line1151"></a>
<a name="line1152"></a>/**
<a name="line1153"></a> * This is used to mark the IE event object so we do not do the Closure pass
<a name="line1154"></a> * twice for a bubbling event.
<a name="line1155"></a> * @param {Event} e The IE browser event.
<a name="line1156"></a> * @private
<a name="line1157"></a> */
<a name="line1158"></a>goog.events.markIeEvent_ = function(e) {
<a name="line1159"></a>  // Only the keyCode and the returnValue can be changed. We use keyCode for
<a name="line1160"></a>  // non keyboard events.
<a name="line1161"></a>  // event.returnValue is a bit more tricky. It is undefined by default. A
<a name="line1162"></a>  // boolean false prevents the default action. In a window.onbeforeunload and
<a name="line1163"></a>  // the returnValue is non undefined it will be alerted. However, we will only
<a name="line1164"></a>  // modify the returnValue for keyboard events. We can get a problem if non
<a name="line1165"></a>  // closure events sets the keyCode or the returnValue
<a name="line1166"></a>
<a name="line1167"></a>  var useReturnValue = false;
<a name="line1168"></a>
<a name="line1169"></a>  if (e.keyCode == 0) {
<a name="line1170"></a>    // We cannot change the keyCode in case that srcElement is input[type=file].
<a name="line1171"></a>    // We could test that that is the case but that would allocate 3 objects.
<a name="line1172"></a>    // If we use try/catch we will only allocate extra objects in the case of a
<a name="line1173"></a>    // failure.
<a name="line1174"></a>    /** @preserveTry */
<a name="line1175"></a>    try {
<a name="line1176"></a>      e.keyCode = -1;
<a name="line1177"></a>      return;
<a name="line1178"></a>    } catch (ex) {
<a name="line1179"></a>      useReturnValue = true;
<a name="line1180"></a>    }
<a name="line1181"></a>  }
<a name="line1182"></a>
<a name="line1183"></a>  if (useReturnValue ||
<a name="line1184"></a>      /** @type {boolean|undefined} */ (e.returnValue) == undefined) {
<a name="line1185"></a>    e.returnValue = true;
<a name="line1186"></a>  }
<a name="line1187"></a>};
<a name="line1188"></a>
<a name="line1189"></a>
<a name="line1190"></a>/**
<a name="line1191"></a> * This is used to check if an IE event has already been handled by the Closure
<a name="line1192"></a> * system so we do not do the Closure pass twice for a bubbling event.
<a name="line1193"></a> * @param {Event} e  The IE browser event.
<a name="line1194"></a> * @return {boolean} True if the event object has been marked.
<a name="line1195"></a> * @private
<a name="line1196"></a> */
<a name="line1197"></a>goog.events.isMarkedIeEvent_ = function(e) {
<a name="line1198"></a>  return e.keyCode &lt; 0 || e.returnValue != undefined;
<a name="line1199"></a>};
<a name="line1200"></a>
<a name="line1201"></a>
<a name="line1202"></a>/**
<a name="line1203"></a> * Counter to create unique event ids.
<a name="line1204"></a> * @type {number}
<a name="line1205"></a> * @private
<a name="line1206"></a> */
<a name="line1207"></a>goog.events.uniqueIdCounter_ = 0;
<a name="line1208"></a>
<a name="line1209"></a>
<a name="line1210"></a>/**
<a name="line1211"></a> * Creates a unique event id.
<a name="line1212"></a> *
<a name="line1213"></a> * @param {string} identifier The identifier.
<a name="line1214"></a> * @return {string} A unique identifier.
<a name="line1215"></a> */
<a name="line1216"></a>goog.events.getUniqueId = function(identifier) {
<a name="line1217"></a>  return identifier + &#39;_&#39; + goog.events.uniqueIdCounter_++;
<a name="line1218"></a>};
<a name="line1219"></a>
<a name="line1220"></a>
<a name="line1221"></a>/**
<a name="line1222"></a> * Expando property for listener function wrapper for Object with
<a name="line1223"></a> * handleEvent.
<a name="line1224"></a> * @type {string}
<a name="line1225"></a> * @private
<a name="line1226"></a> */
<a name="line1227"></a>goog.events.LISTENER_WRAPPER_PROP_ = &#39;__closure_events_fn_&#39; +
<a name="line1228"></a>    ((Math.random() * 1e9) &gt;&gt;&gt; 0);
<a name="line1229"></a>
<a name="line1230"></a>
<a name="line1231"></a>/**
<a name="line1232"></a> * @param {Object|Function} listener The listener function or an
<a name="line1233"></a> *     object that contains handleEvent method.
<a name="line1234"></a> * @return {!Function} Either the original function or a function that
<a name="line1235"></a> *     calls obj.handleEvent. If the same listener is passed to this
<a name="line1236"></a> *     function more than once, the same function is guaranteed to be
<a name="line1237"></a> *     returned.
<a name="line1238"></a> * @private
<a name="line1239"></a> */
<a name="line1240"></a>goog.events.wrapListener_ = function(listener) {
<a name="line1241"></a>  if (goog.isFunction(listener)) {
<a name="line1242"></a>    return listener;
<a name="line1243"></a>  }
<a name="line1244"></a>
<a name="line1245"></a>  return listener[goog.events.LISTENER_WRAPPER_PROP_] ||
<a name="line1246"></a>      (listener[goog.events.LISTENER_WRAPPER_PROP_] = function(e) {
<a name="line1247"></a>        return listener.handleEvent(e);
<a name="line1248"></a>      });
<a name="line1249"></a>};
<a name="line1250"></a>
<a name="line1251"></a>
<a name="line1252"></a>// Register the browser event handler as an entry point, so that
<a name="line1253"></a>// it can be monitored for exception handling, etc.
<a name="line1254"></a>goog.debug.entryPointRegistry.register(
<a name="line1255"></a>    /**
<a name="line1256"></a>     * @param {function(!Function): !Function} transformer The transforming
<a name="line1257"></a>     *     function.
<a name="line1258"></a>     */
<a name="line1259"></a>    function(transformer) {
<a name="line1260"></a>      goog.events.handleBrowserEvent_ = transformer(
<a name="line1261"></a>          goog.events.handleBrowserEvent_);
<a name="line1262"></a>    });
</pre>


</body>
</html>
